//
//  MainViewController.m
//  ProjectForA
//
//  Created by hmengwei on 14/11/19.
//  Copyright (c) 2014年 hmengwei. All rights reserved.
//

#import "MainViewController.h"
#import "DestinationCollectionViewCell.h"
#import "HeaderCollectionReusableView.h"
#import "IntroViewController.h"
#import "ConnectModel.h"
#import "DestinationModel.h"
#import "SmallCollectionViewCell.h"
#import "FlyCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "MytextField.h"
@interface MainViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UISearchBarDelegate>


@end

@implementation MainViewController

- (void)dealloc
{
    [_collect release];
    [_MainArr release];
    [_listArr release];
    [_header release];
    [_scroll release];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.MainArr = [NSMutableArray array];
        self.listArr = [NSMutableArray array];
    }
    return self;
}
//调用网络请求
- (void)createConnect
{
    [ConnectModel getConnectWithUrl:DestinationUrl dic:nil block:^(NSData *data) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = [dic objectForKey:@"elements"];
        [_MainArr addObjectsFromArray:arr];
        
        NSArray *array = [dic objectForKey:@"search_data"];
        [_listArr addObjectsFromArray:array];
        [_collection reloadData];
        [_collect reloadData];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor grayColor];
    
    
    [self rightButton];
    [self createCollectionView];//刚进入的页面
    [self createCollection];//飞下来的页面
    [self select];
    [self createConnect];
    

    
    
}

//搜索框
- (void)select
{

    
    UISearchBar *mySearch = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width / 3 * 2, 30)];
    mySearch.barStyle = UIBarStyleDefault;
    mySearch.barTintColor = [UIColor blackColor];
    mySearch.placeholder = @"请输入搜索内容";
    mySearch.delegate = self;
    mySearch.keyboardType = UIKeyboardTypeDefault;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:mySearch];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    
    
}


-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [UIView beginAnimations:@"ad" context:nil];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationDuration:1];
    [_collection setFrame:CGRectMake(0 , 0, self.view.frame.size.width,self.view.frame.size.height / 2)];
    [UIView commitAnimations];
}


//附近
- (void)rightButton
{
    UIButton *right = [UIButton buttonWithType:UIButtonTypeCustom];
    [right setFrame:CGRectMake(0, 0, 50, 40)];
    [right setTitle:@"附近" forState:UIControlStateNormal];
    [right setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:right];
}

//搜索框下拉框collectionView
- (void)createCollection
{
    
    //布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [layout setItemSize:CGSizeMake(self.view.frame.size.width / 3.4, self.view.frame.size.width / 10)];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    layout.headerReferenceSize = CGSizeMake(200, 40);
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, - (self.view.frame.size.height), self.view.frame.size.width, self.view.frame.size.height - 113) collectionViewLayout:layout];
    [_collection setBackgroundColor:[UIColor whiteColor]];
    _collection.dataSource = self;
    _collection.delegate = self;
    [_collection registerClass:[FlyCollectionViewCell class] forCellWithReuseIdentifier:@"fly"];
    [_collection registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.view addSubview:_collection];
    [_collection release];
    [layout release];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    [UIView beginAnimations:@"ad" context:nil];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationDuration:1];
    [_collection setFrame:CGRectMake(0 , 0, self.view.frame.size.width,self.view.frame.size.height / 2)];
    [UIView commitAnimations];
}



/************************************主页的************************************/
//collectionView
- (void)createCollectionView
{
    //布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [layout setItemSize:CGSizeMake(self.view.frame.size.width / 2.2, self.view.frame.size.width / 2.2)];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
    layout.headerReferenceSize = CGSizeMake(200, 40);
    
    self.collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113) collectionViewLayout:layout];
    [_collect setBackgroundColor:[UIColor whiteColor]];
    _collect.dataSource = self;
    _collect.delegate = self;
    [_collect registerClass:[DestinationCollectionViewCell class] forCellWithReuseIdentifier:@"main"];
    [_collect registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.view addSubview:_collect];
    [_collect release];
    [layout release];
}
//section
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if (collectionView == _collection) {
        return [_listArr count];
    }else
    return [_MainArr count];
}
//items
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == _collection) {
        NSMutableDictionary *dic = [_listArr objectAtIndex:section];
        NSArray *arr = [dic objectForKey:@"elements"];
        return [arr count];
    }else
    {
        
    NSMutableDictionary *dic = [_MainArr objectAtIndex:section];
    NSString *title = [dic objectForKey:@"title"];
        if ([title isEqualToString:@"美洲和大洋洲"] ) {
            return 2;
        }

        return 4;
    }
}

//重用池
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _collection) {
        
        FlyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"fly" forIndexPath:indexPath];
        NSMutableDictionary *bigDic = [_listArr objectAtIndex:indexPath.section];
        NSArray *arr = [bigDic objectForKey:@"elements"];
        NSMutableDictionary *dic = [arr objectAtIndex:indexPath.item];
        NSString *name = [dic objectForKey:@"name"];
        cell.aLabel.text = name;
        return cell;
        
    }else
    {
    
    DestinationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"main" forIndexPath:indexPath];
    NSMutableDictionary *bigdic = [_MainArr objectAtIndex:indexPath.section];
    NSArray *arr = [bigdic objectForKey:@"data"];
    NSMutableDictionary *dic = [arr objectAtIndex:indexPath.item];
    NSString *cover_s = [dic objectForKey:@"cover_s"];
    [cell.aImage setImageWithURL:[NSURL URLWithString:cover_s]];
    cell.aLabel.text = [dic objectForKey:@"name"];
        return cell;
    }
    
    
}


//header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    self.header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
//    [header setBackgroundColor:[UIColor redColor]];
    if (collectionView == _collection) {
        UIFont *font = [UIFont boldSystemFontOfSize:15];
        NSMutableDictionary *dic = [_listArr objectAtIndex:indexPath.section];
        NSString *title = [dic objectForKey:@"title"];
        _header.title.text = title;
        _header.title.textColor = [UIColor blueColor];
        _header.title.font = font;
        return _header;
    }else
    {
    UIFont *font = [UIFont boldSystemFontOfSize:15];
    NSMutableDictionary *dic = [_MainArr objectAtIndex:indexPath.section];
    NSString *title = [dic objectForKey:@"title"];
    _header.title.text = title;
    _header.title.textColor = [UIColor grayColor];
    _header.title.font = font;
    return _header;
    }
}

//跳转
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    IntroViewController *intr = [[IntroViewController alloc] init];
    [self.navigationController pushViewController:intr animated:YES];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
